Skip to content

feat: expose nucleon number, atom mass, density and is-gas accessors (#119)#124

Open
grzanka wants to merge 3 commits into
mainfrom
claude/libdedx-migration-plan-11oX6
Open

feat: expose nucleon number, atom mass, density and is-gas accessors (#119)#124
grzanka wants to merge 3 commits into
mainfrom
claude/libdedx-migration-plan-11oX6

Conversation

@grzanka
Copy link
Copy Markdown
Contributor

@grzanka grzanka commented Jun 4, 2026

Closes #119 · Phase A of the dedx_extra.c migration epic #118.

What

Promotes four previously-internal helpers to the public API in dedx.h, so frontends/bindings (notably the WASM web app) no longer need their own shims:

int   dedx_get_nucleon_number(int ion, int *err);      /* mass number A */
float dedx_get_atom_mass(int ion, int *err);           /* atomic mass, u */
float dedx_get_density(int material, int *err);        /* g/cm³ */
int   dedx_is_gas(int target, int *err);               /* 1 = gas, 0 = condensed/unknown */

The implementations already existed inside libdedx (dedx_internal_get_nucleon/_atom_mass in dedx_periodic_table.c, dedx_internal_read_density/_target_is_gas in dedx_data_access.c) — they just lacked a public header entry.

Why

dedx_web/wasm/dedx_extra.c re-exports exactly these four functions because they had no public declaration. This is the smallest, lowest-risk slice of the migration plan and lets the web side delete its shims. It also unblocks dedx_web #175 (display selected-material density) and #176 (custom density).

Notable detail — a latent out-of-bounds guard

dedx_internal_get_nucleon() / dedx_internal_get_atom_mass() only guard the upper bound (id < 113); for ion <= 0 they would index dedx_nucl[id-1] / dedx_amu[id-1] at a negative offset. The new public element accessors add the missing lower-bound check and return -1 with DEDX_ERR_NOT_AN_ELEMENT for out-of-range elements. dedx_get_density propagates DEDX_ERR_TARGET_NOT_FOUND for unknown materials; dedx_is_gas normalises the internal size_t to 0/1 and reports unknown targets as non-gas (documented).

This partially addresses the "bounds-check name/lookup accessors" item in the health-review epic #108.

Tests & docs

  • tests/test_accessors.c (new, auto-registered by the test_*.c glob): valid values (H/He/C nucleon numbers and masses, water/air density, liquid-vs-gas), out-of-range guards (0, negative, 113, 200), and the unknown-material error path. Verified against the real embedded data: Carbon A=12 / 12.0107 u, water 1.0 g/cm³ (non-gas), air 1.205e-3 g/cm³ (gas).
  • Short usage demo added to examples/dedx_list.c.

Verification

  • ctest: 28/28 pass (27 existing + the new test).
  • clang-format --Werror: clean on all changed files.
  • clang-tidy (the CI config, WarningsAsErrors: '*'): no findings in the added code.

Open question for maintainers

Final function names — I followed the names proposed in #119 and the dedx_get_* convention. Happy to rename (e.g. dedx_get_ion_nucleon_number to match the web shim, or drop get_ for the predicate) if you prefer.


Authored via Claude Code as Phase A of the dedx_extra.c → libdedx migration.


Generated by Claude Code

…119)

Promote four previously-internal helpers to the public API so frontends and
bindings no longer need their own shims:

  int   dedx_get_nucleon_number(int ion, int *err);
  float dedx_get_atom_mass(int ion, int *err);
  float dedx_get_density(int material, int *err);
  int   dedx_is_gas(int target, int *err);

The element accessors add the lower-bound check the internal lookups lack
(dedx_internal_get_nucleon/_atom_mass only guard the upper bound and would
index the table at a negative offset for ion <= 0); out-of-range elements now
return -1 with DEDX_ERR_NOT_AN_ELEMENT. dedx_get_density propagates
DEDX_ERR_TARGET_NOT_FOUND for unknown materials; dedx_is_gas normalises the
internal size_t result to 0/1.

Adds tests/test_accessors.c (valid values, out-of-range guards, liquid vs gas)
and a short usage demo in examples/dedx_list.c.

https://claude.ai/code/session_01XdTFfBgkL344BjVrxARiNc
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

❌ Patch coverage is 88.09524% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.66%. Comparing base (335be0b) to head (7d2e906).

Files with missing lines Patch % Lines
tests/test_accessors.c 85.71% 6 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #124      +/-   ##
==========================================
+ Coverage   77.27%   77.66%   +0.38%     
==========================================
  Files          30       31       +1     
  Lines        3019     3103      +84     
  Branches      403      409       +6     
==========================================
+ Hits         2333     2410      +77     
- Misses        597      600       +3     
- Partials       89       93       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR exposes four previously-internal material/ion property helpers as part of the public C API (include/dedx.h), to support downstream frontends/bindings (notably the WASM app) without local shims. It adds bounds-checking for element-table access and introduces a new test covering the new accessors, plus a short usage demo in an example.

Changes:

  • Add public API declarations for nucleon number, atomic mass, density, and gas/condensed flag accessors.
  • Implement the new public accessors in src/dedx.c, including lower-bound guards for element lookups.
  • Add a new dedicated test (tests/test_accessors.c) and update examples/dedx_list.c with a usage snippet.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
include/dedx.h Adds public declarations + Doxygen docs for the four new accessors.
src/dedx.c Implements public wrappers and adds element-range guarding for mass/nucleon lookups.
tests/test_accessors.c New test file validating correct values and error/sentinel behavior.
examples/dedx_list.c Adds a short demo showing how to call the new accessors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/dedx_list.c Outdated
Comment thread examples/dedx_list.c Outdated
Comment thread examples/dedx_list.c Outdated
Comment thread include/dedx.h
Comment thread tests/test_accessors.c Outdated
- examples/dedx_list.c: read each accessor result into a temporary before
  printing. Passing one &err to several calls inside a single printf left the
  writes to *err unsequenced (argument evaluation order is unspecified in C).
- dedx.h: clarify that dedx_is_gas always sets *err = DEDX_OK; an unknown
  target is reported as non-gas, not as an error.
- test_accessors.c: assert *err == DEDX_OK for the unknown-target is_gas case
  to lock in the documented contract.

https://claude.ai/code/session_01XdTFfBgkL344BjVrxARiNc
Copy link
Copy Markdown
Contributor Author

grzanka commented Jun 4, 2026

Addressed the review feedback in c70e206:

  • Unsequenced *err writes in the example (3 comments). examples/dedx_list.c now reads each accessor result into a temporary before printing, so no single printf passes one &err to multiple calls.
  • dedx_is_gas doc contract. Clarified in dedx.h that *err is always set to DEDX_OK — an unknown target is reported as non-gas, not as an error.
  • Test contract. test_accessors.c now also asserts *err == DEDX_OK for the unknown-target is_gas case.

clang-format --Werror clean, ctest 28/28 pass, example output unchanged.


Generated by Claude Code

…helpers

Route every assertion through three small expect_int/expect_near/expect_true
helpers (file-scope failure counter) instead of per-call faili/failmsg/
check_close + inline failmsg() lines. This removes the scattered failure-only
statements that showed up as uncovered patch lines; the only lines now
uncovered on a passing run are the three shared helper failure branches.

Also adds positive cases (oxygen mass, Z=112 upper-boundary acceptance) that
exercise more real code paths. test_accessors.c line coverage 87% -> 92%
(remaining 8% is the irreducible assert-failed branches); dedx.c accessors
remain fully covered with both guard branches exercised. 28/28 ctest pass.

https://claude.ai/code/session_01XdTFfBgkL344BjVrxARiNc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose nucleon number, atom mass, density and is-gas in the public header

3 participants